package com.leoandroid.tool.toolsbox.largeread.fragment

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.leoandroid.tool.toolsbox.ColorListActivity
import com.leoandroid.tool.toolsbox.FengJingActivity
import com.leoandroid.tool.toolsbox.ImageColorActivity
import com.leoandroid.tool.toolsbox.ImageWaterActivity
import com.leoandroid.tool.toolsbox.ToolCompressActivity
import com.leoandroid.tool.toolsbox.camera.RecognizeCameraActivity
import com.leoandroid.tool.toolsbox.camera.utils.Constants
import com.leoandroid.tool.toolsbox.largeread.activity.SurfaceCameraActivity
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityPictureColorBinding
import com.leoandroid.tool.toolsbox.largeread.databinding.FragmentToolHomeBinding
import com.leoandroid.tool.toolsbox.largeread.utils.FlashlightUtils
import com.leoandroid.tool.toolsbox.largeread.utils.SPUtil
import com.leoandroid.tool.toolsbox.largeread.utils.ToolUtils
import com.leoandroid.tool.toolsbox.new.DriverQuestionActivity
import com.leoandroid.tool.toolsbox.new.JiTangActivity
import android.provider.Settings
import com.leoandroid.tool.toolsbox.largeread.activity.RulerActivity

private const val requestCode_Camera = 1

class HomeFragment : BaseFragment() {

    var cameraPer = arrayOf(Manifest.permission.CAMERA)

    companion object {
        fun getInstance(): HomeFragment {
            return HomeFragment()
        }
    }

    var bindingF: FragmentToolHomeBinding? = null
    val binding get() = bindingF!!
    private var isLighOn = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        bindingF = FragmentToolHomeBinding.inflate(inflater, container, false)
        if (ToolUtils.getHeightStatusBar(context) > 0) {
            binding.layoutPadding.setPadding(
                0,
                ToolUtils.getHeightStatusBar(context),
                0,
                0
            )
        }

//        //放大镜
//        binding.topFunc1.setOnClickListener {
//            val cameraDenied = SPUtil.with(activity).load().read("CAMERA_DENIED", false)
//            if (cameraDenied) {
//                Toast.makeText(activity, "没有相机授权，功能使用受限,请先授权", Toast.LENGTH_SHORT)
//                    .show()
//                return@setOnClickListener
//            }
//            val camera = SPUtil.with(activity).load().read("CAMERA", false)
//            if (!camera) {
//                //申请相机权限
//                activity?.let { it1 ->
//                    ActivityCompat.requestPermissions(
//                        it1,
//                        cameraPer,
//                        requestCode_Camera
//                    )
//                }
//                return@setOnClickListener
//            }
//            jumpCamera()
//        }

        //图片取色
        binding.funcQs.setOnClickListener {
            val intent = Intent(context, ImageColorActivity::class.java)
            startActivity(intent)
        }

        //驾考宝典
        binding.funcJkbd.setOnClickListener {
            val intent = Intent(context, DriverQuestionActivity::class.java)
            startActivity(intent)
        }

        //心灵鸡汤
        binding.funcXljt.setOnClickListener {

            val intent = Intent(context, JiTangActivity::class.java)
            startActivity(intent)
        }

//        //文字识别
//        binding.funcWzsb.setOnClickListener {
//            val intent = Intent(context, RecognizeCameraActivity::class.java)
//            intent.putExtra(Constants.RECOGNIZE_TYPE, Constants.RECOGNIZE_TYPE_ACCURATE)
//            startActivity(intent)
//        }

//        //颜色大全
//        binding.funcYsdq.setOnClickListener {
//            val intent = Intent(context, ColorListActivity::class.java)
//            startActivity(intent)
//        }

//        //景区查询
//        binding.funcJqcx.setOnClickListener {
//            val intent = Intent(context, FengJingActivity::class.java)
//            startActivity(intent)
//
//        }
//        //图片压缩
//        binding.funcTpys.setOnClickListener {
//            val intent = Intent(context, ToolCompressActivity::class.java)
//            startActivity(intent)
//
//        }
        //图片水印
        binding.funcTpsy.setOnClickListener {
            val intent = Intent(context, ImageWaterActivity::class.java)
            startActivity(intent)

        }
        //植物识别
        binding.funcZwsb.setOnClickListener {
            val intent = Intent(context, RecognizeCameraActivity::class.java)
            intent.putExtra(Constants.RECOGNIZE_TYPE, Constants.RECOGNIZE_TYPE_PLANT)
            startActivity(intent)
        }

//        //水果识别
//        binding.funcSgsb.setOnClickListener {
//            val intent = Intent(context, RecognizeCameraActivity::class.java)
//            intent.putExtra(Constants.RECOGNIZE_TYPE, Constants.RECOGNIZE_TYPE_DISH)
//            startActivity(intent)
//        }
//        // 尺子测量
//        binding.funcCzcl.setOnClickListener {
//            val intent = Intent(context, RulerActivity::class.java)
//            startActivity(intent)
//        }
//        // 放大镜
//        binding.funcFdj.setOnClickListener {
//            val cameraDenied = SPUtil.with(activity).load().read("CAMERA_DENIED", false)
//            if (cameraDenied) {
//                Toast.makeText(activity, "没有相机授权，功能使用受限,请先授权", Toast.LENGTH_SHORT)
//                    .show()
//                return@setOnClickListener
//            }
//            val camera = SPUtil.with(activity).load().read("CAMERA", false)
//            if (!camera) {
//                //申请相机权限
//                activity?.let { it1 ->
//                    ActivityCompat.requestPermissions(
//                        it1,
//                        cameraPer,
//                        requestCode_Camera
//                    )
//                }
//                return@setOnClickListener
//            }
//            jumpCamera()
//        }

        return binding.root
    }

    private fun turnOnFlash() {
        FlashlightUtils.linghtOn()
        isLighOn = true
    }

    // Turning Off flash
    private fun turnOffFlash() {
        FlashlightUtils.linghtOff()
        isLighOn = false
    }

    fun jumpCamera() {
        val intent = Intent(context, SurfaceCameraActivity::class.java)
        startActivity(intent)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
    }

    override fun initView(view: View) {
    }
}